<?php
require_once("nusoap.php"); 
require_once("bootstrap.php");
class request_builder
{
      var $num_adt;
      var $num_chd;
      var $num_inf;
      var $cab_cla;
      var $origin_code;
      var $dest_code;
      var $search_type;
      var $dep_date;
      var $ret_date;
      var $req_fet;
      var $request_id;
      var $xml_request;
      var $req;
      var $params;
      var $header;
      var $operation;
      var $namespace;
  function request_builder($adt,$chd,$inf,$cabin,$ori,$dest,$srt,$depdate,$retdate,$request_id){
        $this->num_adt=$adt;
        $this->num_chd=$chd;
        $this->num_inf=$inf;
        $this->cab_cla=$cabin;
        $this->origin_code=$ori;
        $this->dest_code=$dest;
        $this->dep_date=$depdate;
        $this->ret_date=$retdate;
        $this->search_type=$srt;
        $this->namespace="xxs";
        $this->header;
        $this->operation="216.113.130.252:8800";
        $this->request_id=$request_id;
        $this->req=Doctrine::getTable('Requests')->find($this->request_id);
        $this->build_request($this->req);
        
      }  
  private function build_request($arr){
      //print_r($arr->toArray());
         $str="<ns1:ProviderTransaction xmlns:ns1='xxs'><REQ><PSC5>";
          $opt_arr=explode(",",$arr->options);
          $str.="<OPT>O</OPT><OPT>H</OPT>";
          foreach($opt_arr as $opt){
          $str.="<OPT>$opt</OPT>";  
          }
          
          $str.="<POI_ORI><CIT>$this->origin_code</CIT></POI_ORI>";
          $str.="<NUM_ALT>".$arr->num_alt."</NUM_ALT>";
          $inc_arl_arr=explode(",",$arr->inc_arl);
          $exc_arl_arr=explode(",",$arr->exc_arl);
          if((count($inc_arl_arr)>=1||count($exc_arl_arr)>=1)&&($arr->inc_arl!=''||$arr->exc_arl!=''))
          {
              $str.="<ARL_INF>";
              if(count($exc_arl_arr)>0&&$arr->exc_arl!='')
              {
                  $str.="<EXC_ARL>";
                  foreach($exc_arl_arr as $exc)
                  {
                      $str.="<ARL_COD>".$exc."</ARL_COD>";
                  }
                  $str.="</EXC_ARL>";
              }
              if(count($inc_arl_arr)>0&&$arr->inc_arl!='')
              {
                  $str.="<ARL_OPT>".$arr->arl_opt."</ARL_OPT>";
                  foreach($inc_arl_arr as $inc)
                  {
                      $str.="<ARL_COD>".$inc."</ARL_COD>";
                  }
                  
              }
            $str.="</ARL_INF>";  
          }
            $rdate=$this->normalize_date($this->ret_date);
             $ddate=$this->normalize_date($this->dep_date);
          
        $ptc_arr=explode(",",$arr->ptc);
        $ptc = "<PTC_INF><NUM_PAX>$this->num_adt</NUM_PAX><PTC>$ptc_arr[0]</PTC></PTC_INF>";
        if($this->num_chd>0){
        $ptc .= "<PTC_INF><NUM_PAX>$this->num_chd</NUM_PAX><PTC>$ptc_arr[1]</PTC></PTC_INF>";
        }
        if($this->num_inf>0){
        $ptc .= "<PTC_INF><NUM_PAX>$this->num_inf</NUM_PAX><PTC>$ptc_arr[2]</PTC></PTC_INF>";
        }
        $str.="<WIN>12</WIN>";
        $str.=$ptc;
        $con_city_cod=explode(",",$arr->con_city_cod);
        
        $str.="<DES_INF>";
        
        /*if(count($con_city_cod)>0&&$arr->con_city_cod!='')
        {
            foreach($con_city_cod as $ele)
            {
                $str.="<CON_CIT_COD><CIT>$ele</CIT></CON_CIT_COD>";
            }
        }*/
        $str.="<DEP_DAT>$ddate</DEP_DAT><DEP_TIM>1200</DEP_TIM><CAB_CLA>Y</CAB_CLA><POI_DES><CIT>$this->dest_code</CIT></POI_DES></DES_INF>";
        if($this->search_type=="RT")
          {
           $str.="<DES_INF><DEP_DAT>$rdate</DEP_DAT><DEP_TIM>1200</DEP_TIM><CAB_CLA>Y</CAB_CLA><POI_DES><CIT>$this->origin_code</CIT></POI_DES></DES_INF>";
          }
        
        $str.="</PSC5></REQ></ns1:ProviderTransaction>";
        $this->header="<t:Transaction xmlns:t='xxs'><tc><iden u='ett' p='ett0000' /><provider session='W1XML' pcc='".$arr->sid."'>Worldspan</provider><trace></trace></tc></t:Transaction>";
       $this->params=$str;
        
      }
  private function normalize_date($date) {
        $date = explode('/', $date);
        $date_mon = $date[0];
        $date_dat = $date[1];
        $date_yea = $date[2];
        switch ($date_mon) {
            case 1:
                $date_mon = "JAN";
                break;
            case 2:
                $date_mon = "FEB";
                break;
            case 3:
                $date_mon = "MAR";
                break;
            case 4:
                $date_mon = "APR";
                break;
            case 5:
                $date_mon = "MAY";
                break;
            case 6:
                $date_mon = "JUN";
                break;
            case 7:
                $date_mon = "JUL";
                break;
            case 8:
                $date_mon = "AUG";
                break;
            case 9:
                $date_mon = "SEP";
                break;
            case 10:
                $date_mon = "OCT";
                break;
            case 11:
                $date_mon = "NOV";
                break;
            case 12:
                $date_mon = "DEC";
                break;
        }
        $date = $date_dat.$date_mon;
        return $date;
    }
  public function test_request()     
    {
       // echo htmlspecialchars($this->params);
      $client= new nusoap_client("https://xmlpro.worldspan.com:443");
      if($client->fault)
      { 
          echo $client->faultstring;
      }
      
      $client->timeout=5000;
      $client->response_timeout=5000;
      $result = $client->call($this->operation, $this->params, $this->namespace, null, $this->header, null, null, null);
      //print_r($result);  
      
      
      //echo htmlspecialchars($client->response);
      $sxe=simplexml_load_string($client->responseData);
      $arr=$sxe->xpath("//ALT_INF");
      if(count($arr)>0)
      {
           //$ar=array();
           //$ar["status"]="SUCCESS";
           //$ar["num_alt"]=count($arr);
           //return json_encode($ar);
		   
		   $a="Status: Success<br> Alt: ".count($arr)."<br>RESPONSE:<br/> <textarea name='text' style='width:580px; height:100px;'>".htmlspecialchars($client->responseData)."</textarea><br>REQUEST:<br/> <textarea name='text' style='width:580px;margin-top:5px; height:100px;'>".htmlspecialchars($client->request)."</textarea>";
		   
		   return $a;
		   
      }
      else
      {
          //$ar=array();
          //$ar["status"]="ERROR";
          //$ar["num_alt"]=0;
          //$ar["reason"]=htmlspecialchars($client->responseData);
		    $a="Status: Success<br> Alt: ".count($arr)."<br>RESPONSE:<br/> <textarea name='text' style='width:580px; height:100px;'>".htmlspecialchars($client->responseData)."</textarea><br>REQUEST:<br/> <textarea name='text' style='width:580px;margin-top:5px; height:100px;'>".htmlspecialchars($client->request)."</textarea>";
			return $a;
      }
      
    }
  }
?>
